package cn.xinfei.xdecision.manager.controller.component.rulebase;

import cn.xinfei.xdecision.common.model.common.PageResult;
import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.common.requestParam.AssemblyRuleCodeReq;
import cn.xinfei.xdecision.common.model.component.guiderule.RuleVersion;
import cn.xinfei.xdecision.common.model.component.guiderule.vo.RuleVersionRespVo;
import cn.xinfei.xdecision.common.model.component.guiderule.vo.RuleVersionVo;
import cn.xinfei.xdecision.common.service.component.guiderule.RuleVersionService;
import cn.xinfei.xdecision.common.utils.constant.OperateNameConst;
import cn.xinfei.xdecision.common.utils.constant.OperateTypeEnum;
import cn.xinfei.xdecision.manager.web.aop.ArchivesLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "组件中心-规则库")
@Controller
@ResponseBody
@RequestMapping("/v3/ruleVersion")
public class RuleVersionController {

    @Autowired
    private RuleVersionService versionService;

    @PostMapping("/getRuleVersionInfo/{versionId}")
    @ApiOperation(value = "查询规则版本详情", notes = "查询规则版本详情")
    public ResponseEntityDto getRuleVersionInfo(@PathVariable Long versionId) {
        RuleVersionVo version =versionService.queryById(versionId);
        return ResponseEntityBuilder.buildNormalResponse(version);
    }

    @PostMapping("/addRuleVersion")
    @ApiOperation(value = "新建版本", notes = "新建版本")
    @ArchivesLog(type = OperateTypeEnum.CREATE,name = OperateNameConst.ComponentOpt.SAVE_VERSION)
    public ResponseEntityDto<Long> addRuleVersion(@RequestBody  RuleVersionVo version){
        return ResponseEntityBuilder.buildNormalResponse(versionService.checkAndAddVersion(version));
    }


    @PostMapping("/updateRuleVersion")
	@ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.ComponentOpt.UPDATE_VERSION)
    public ResponseEntityDto updateRuleVersion(@RequestBody RuleVersionVo version){
        boolean b = versionService.updateVersion(version);
        List<RuleVersionVo> ruleVersionList = versionService.queryVersionListByRuleId(version.getRuleId());
        return ResponseEntityBuilder.buildNormalResponse(ruleVersionList);
    }


	@ApiOperation(value = "锁定版本")
	@PostMapping(value = "/lock/{versionId}")
	@ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.ComponentOpt.LOCK_VERSION)
	public ResponseEntityDto<String> lock(@PathVariable Long versionId) {
		versionService.lock(versionId);
		return ResponseEntityBuilder.buildNormalResponse("锁定版本成功!");
	}

	@ResponseBody
	@RequestMapping(value = "queryVersionInfos", method = RequestMethod.POST)
	@ApiOperation(value = "查询复杂规则版本列表", notes = "查询复杂规则版本列表")
	public ResponseEntityDto<PageResult<List<RuleVersionRespVo>, RuleVersion>> getRuleInfo(@Valid @RequestBody AssemblyRuleCodeReq req) {
		return versionService.listByRuleCode(req);
	}

}
